//
//  AELAddTrackControlController.swift
//  ActualExaminationLine
//
//  Created by iOS-Czz on 2023/10/30.
//

import UIKit
import WMZDialog
class AELAddTrackControlController: AELBaseViewController{
    
    @IBOutlet var bookingView: UIView!
    @IBOutlet var practiceView: UIView!
    @IBOutlet var placeTF: UITextField!
   
    @IBOutlet var dateTF: UITextField!
    @IBOutlet var timeTF: UITextField!
    var type = 6
    @IBOutlet var durationTF: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        tapPracticeReservationAction("")
        // Do any additional setup after loading the view.
    }
    
    @IBAction func tapPracticeReservationAction(_ sender: Any) {
        self.type = 0
        self.practiceView.layer.borderWidth = 2
        self.bookingView.layer.borderWidth = 0
        self.practiceView.backgroundColor = #colorLiteral(red: 0.2276425362, green: 0.7226418853, blue: 0.9993616939, alpha: 1)
        self.bookingView.backgroundColor = #colorLiteral(red: 0.09513626248, green: 0.35506621, blue: 0.5184001923, alpha: 1)
    }
    @IBAction func tapTestBookingAction(_ sender: Any) {
        self.type = 1
        self.practiceView.layer.borderWidth = 0
        self.bookingView.layer.borderWidth = 2
        self.practiceView.backgroundColor = #colorLiteral(red: 0.09513626248, green: 0.35506621, blue: 0.5184001923, alpha: 1)
        self.bookingView.backgroundColor = #colorLiteral(red: 0.2276425362, green: 0.7226418853, blue: 0.9993616939, alpha: 1)
    }
    @IBAction func clickCloseAction(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.wx_navigationBar.isHidden = true
    }
    @IBAction func clockSaveAction() {
        self.view.endEditing(true)
        if self.placeTF.text!.isEmpty {
            UIView.showTipsView("Please enter booking place")
            return
        }
        if self.dateTF.text!.isEmpty {
            UIView.showTipsView("Please select appointment date")
            return
        }
        if self.timeTF.text!.isEmpty {
            UIView.showTipsView("Please select appointment time")
            return
        }
        if self.durationTF.text!.isEmpty {
            UIView.showTipsView("Please enter reservation duration")
            return
        }
        let model = AELTrackControlModel()
        model.type = self.type
        model.name = self.placeTF.text!
        model.time = self.dateTF.text! + self.timeTF.text!
        model.duration = self.durationTF.text!
        model.isDefault = false
        model.saveToDB()
        UIView.showResultView("Add reservation succeed", true)
        let helper = AELTrackControlModel.getUsingLKDBHelper()
        helper.delete(with: AELTrackControlModel.self, where: "isDefault = true")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popViewController(animated: true)
        }
    }
    @IBAction func tapDateAction(_ sender: Any) {
        self.view.endEditing(true)
        let dialog = Dialog()
        dialog.wDateTimeTypeSet()("yyyy.MM.dd")
        .wDefaultDateSet()(Date())
        .wCancelTitleSet()("Cancel")
        .wOKTitleSet()("Ok")
        .wEventOKFinishSet()({(anyId:Any?,otherData:Any?)in
            self.dateTF.text = otherData as? String
        })
        .wTypeSet()(DialogTypeDatePicker);
        _ = dialog.wStart()
    }
    @IBAction func tapTimeAction(_ sender: Any) {
        self.view.endEditing(true)
        let dialog = Dialog()
        dialog.wDateTimeTypeSet()("HH:mm")
        .wDefaultDateSet()(Date())
        .wCancelTitleSet()("Cancel")
        .wOKTitleSet()("Ok")
        .wEventOKFinishSet()({(anyId:Any?,otherData:Any?)in
            self.timeTF.text = otherData as? String
        })
        .wTypeSet()(DialogTypeDatePicker);
        _ = dialog.wStart()
    }
    
}
